package com.vsd.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.dialet.PaginationSupport;
import com.demo.util.JSONUtils;
import com.demo.util.StringUtil;
import com.demo.validator.AdminLoginValidate;
import com.vsd.daoEx.model.IndexActivityEx;
import com.vsd.domain.IndexActivity;
import com.vsd.service.IndexActivityService;

@Controller
@RequestMapping("indexActivity")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class IndexActivityController extends CoreAdminController{
	@Autowired
	private IndexActivityService indexActivityService;
 
	
	@RequestMapping("/list")
	@ResponseBody
	public String citylist(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,HttpServletRequest request){
		
		PaginationSupport.byPage(start/length+1, length);
		List<IndexActivityEx> list = indexActivityService.selectExList(areaCode);
		
		return JSONUtils.serialize(new DataTable(draw+1,PaginationSupport.getContext().getTotalCount(), list));
	}
	
	@RequestMapping("/listByStore")
	@ResponseBody
	public String listByStore(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,HttpServletRequest request){
		areaCode = getSessionParameter("areaCode", session);
		
		PaginationSupport.byPage(1, 5);
		List<IndexActivityEx> list = indexActivityService.selectNowList(areaCode,getSessionParameter("id", session));
		
		return JSONUtils.serialize(new DataTable(draw+1,5, list));
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(IndexActivity act){
		if(StringUtils.isBlank(act.getId())){
			//是否已推荐
			act.setId(StringUtil.getUUID());
			act.setImgType(1);
			act.setState(0);
			act.setCreateTime(new Date());
			indexActivityService.save(act);
		}else{
			indexActivityService.updateByPrimaryKey(act);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/findById")
	@ResponseBody
	public String findById(String id){
		IndexActivity r = indexActivityService.findOne(id);
		return JSONUtils.serialize(r);
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String string : ids) {
			indexActivityService.delById(string);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	 
	
}
